home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / util / AbstractSet.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  3.5 KB  |  106 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)AbstractSet.java    1.8 98/09/30
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.util;
  16.  
  17. /**
  18.  * This class provides a skeletal implementation of the <tt>Set</tt>
  19.  * interface to minimize the effort required to implement this
  20.  * interface. <p>
  21.  *
  22.  * The process of implementing a set by extending this class is identical
  23.  * to that of implementing a Collection by extending AbstractCollection,
  24.  * except that all of the methods and constructors in subclasses of this
  25.  * class must obey the additional constraints imposed by the <tt>Set</tt>
  26.  * interface (for instance, the add method must not permit addition of
  27.  * multiple intances of an object to a set).<p>
  28.  *
  29.  * Note that this class does not override any of the implementations from
  30.  * the <tt>AbstractCollection</tt> class.  It merely adds implementations
  31.  * for <tt>equals</tt> and <tt>hashCode</tt>.
  32.  *
  33.  * @author  Josh Bloch
  34.  * @version 1.8 09/30/98
  35.  * @see Collection
  36.  * @see AbstractCollection
  37.  * @see Set
  38.  * @since JDK1.2
  39.  */
  40.  
  41. public abstract class AbstractSet extends AbstractCollection implements Set {
  42.     /**
  43.      * Sole constructor.  (For invocation by subclass constructors, typically
  44.      * implicit.)
  45.      */
  46.     protected AbstractSet() {
  47.     }
  48.  
  49.     // Comparison and hashing
  50.  
  51.     /**
  52.      * Compares the specified object with this set for equality.  Returns
  53.      * <tt>true</tt> if the given object is also a set, the two sets have
  54.      * the same size, and every member of the given set is contained in
  55.      * this set.  This ensures that the <tt>equals</tt> method works
  56.      * properly across different implementations of the <tt>Set</tt>
  57.      * interface.<p>
  58.      *
  59.      * This implementation first checks if the specified object is this
  60.      * set; if so it returns <tt>true</tt>.  Then, it checks if the
  61.      * specified object is a set whose size is identical to the size of
  62.      * this set; if not, it it returns false.  If so, it returns
  63.      * <tt>containsAll((Collection) o)</tt>.
  64.      *
  65.      * @param o Object to be compared for equality with this set.
  66.      * @return <tt>true</tt> if the specified object is equal to this set.
  67.      */
  68.     public boolean equals(Object o) {
  69.     if (o == this)
  70.         return true;
  71.  
  72.     if (!(o instanceof Set))
  73.         return false;
  74.     Collection c = (Collection) o;
  75.     if (c.size() != size())
  76.         return false;
  77.     return containsAll(c);
  78.     }
  79.  
  80.     /**
  81.      * Returns the hash code value for this set.  The hash code of a set is
  82.      * defined to be the sum of the hash codes of the elements in the set.
  83.      * This ensures that <tt>s1.equals(s2)</tt> implies that
  84.      * <tt>s1.hashCode()==s2.hashCode()</tt> for any two sets <tt>s1</tt>
  85.      * and <tt>s2</tt>, as required by the general contract of
  86.      * Object.hashCode.<p>
  87.      *
  88.      * This implementation enumerates over the set, calling the
  89.      * <tt>hashCode</tt> method on each element in the collection, and
  90.      * adding up the results.
  91.      *
  92.      * @returns the hash code value for this set.
  93.      */
  94.     public int hashCode() {
  95.     int h = 0;
  96.     Iterator i = iterator();
  97.     while (i.hasNext()) {
  98.         Object obj = i.next();
  99.             if (obj != null)
  100.                 h += obj.hashCode();
  101.         }
  102.     return h;
  103.     }
  104. }
  105.  
  106.